﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using HZ.BidSystem.Model;

namespace HZ.BidSystem.BLL
{
    /// <summary>
    /// 解密服务层
    /// </summary>
    public static class BLL_Service
    {
        /// <summary>
        /// 当前解密伺服线程
        /// </summary>
        public static Thread thread = new Thread(new ThreadStart(DecPriceService));
        /// <summary>
        /// 解密队列
        /// </summary>
        private static Queue<DecModel> quene = new Queue<DecModel>();

        /// <summary>
        /// 对解密增加一个队列
        /// </summary>
        /// <param name="model">解密实体</param>
        public static void Enqueue(DecModel model)
        {
            if (!SuccessList.Contains(model.CompanyId))
            {
                AllList.Add(model.CompanyId);
                quene.Enqueue(model);
            }
        }

        /// <summary>
        /// 成功队列
        /// </summary>
        public static List<int> SuccessList = new List<int>();

        /// <summary>
        /// 失败队列
        /// </summary>
        public static List<int> FailList = new List<int>();


        /// <summary>
        /// 所有队列
        /// </summary>
        public static List<int> AllList = new List<int>();

        /// <summary>
        /// 解密项目编号
        /// </summary>
        public static Int32 ProjectId { get; set; }



        private static void DecPriceService()
        {
            while (true)
            {
                if (quene.Count > 0)
                {
                    var DecModel = quene.Dequeue();

                    if (BLL_Factory.IBLL_Company.DecPrice(DecModel.CompanyId, 1, DecModel.BidPwd, null, BidType.Bid) == 1)
                    {
                        SuccessList.Add(DecModel.CompanyId);
                    }
                    else
                    {
                        FailList.Add(DecModel.CompanyId);
                    }
                }
                else
                {
                    Thread.Sleep(1000);
                }

            }
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <returns></returns>
        public static int Start()
        {
            if (thread.ThreadState != ThreadState.Running)
            {

                thread.Start();
            }
            return 1;
        }

        /// <summary>
        /// 终止服务
        /// </summary>
        /// <returns></returns>
        public static int Stop()
        {
            try
            {
                thread.Abort();

            }
            catch (ThreadStateException)
            {

            }
            catch (Exception ee)
            {
                HZ.Common.log4netManager.LogDebug("", "", ee);
            }
            return 1;
        }
    }
}
